<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%@ taglib uri="/sinopac-fn" prefix="fn" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<link rel="stylesheet" href="/sinopac/form/CSS/tabs.css">
		<script type="text/javascript" src="/sinopac/js/jQuery/jquery-1.8.2.min.js"></script>
		<script type="text/javascript" src="/sinopac/js/jQuery/jquery_DOMWindow.js"></script>
		<script type="text/javascript" src="/sinopac/js/tools/formUtil.js"></script>
<script>
	$(document).ready(function(){
		$('#token_esn').click(function(){
			var self = $(this);
			var voValue = self.attr('voValue');
			var form = $('#form1');
			form.prop('action', '${fn:url('/Index/chkCustId.action')}');
			form.append('<input type="hidden" name="function_num" value="4" />');
			form.append('<input type="hidden" name="token_esn" value="'+voValue+'" />');
			form.submit();
		});

		$("input[name=upmd]").click(function(){
			var url = "${fn:url('/CustomerApply/updateData')}";
			var token_esn = $("#token_esn").val();
			var customer_contact = $("input[name=customer_contact]").val();
			var customer_phone = $("input[name=customer_phone]").val();
			var customer_address = $('#customer_address').val();
			var mailarea = $("input[name=mailarea]").val();
    		var mailbarcode = $("input[name=mailbarcode]").val();
			$.ajax({  
                type: "post",  
                url: url,  
                dataType: "json",  
                data: {"token_esn":token_esn,
                	   "customer_contact":customer_contact,
                	   "customer_phone":customer_phone,
                	   "customer_address":customer_address,
                	   "mailarea":mailarea,
                	   "mailbarcode":mailbarcode},
                success: function(){ alert("客戶資料更新完成！"); },
                error: function(){ alert("客戶資料更新失敗！ 請檢查客戶資料內容是否正確"); } 
            }); 
		});
	});
	
	function chkApply(){
		if($('#is_group_main').val() == "0" && $('#is_group').val() == "1"){
			alert("此帳戶並非主帳戶，無法進行申請配發TOKEN ESN");
		}else{
			if(<s:property value="token_flag"/>){
				$.openDOMWindow({
			    	windowSourceID: '#errorForm',
			        height:60,  
			        width:210, 
			        overlay:1, 
			        overlayColor: '#A9A9A9',
			        windowPadding:20,
			        modal:1,
			        borderColor:'steelblue',
			        borderSize:'3', 
			        positionLeft:120, 
			        positionTop:-150,
			    });
			}else{
				var form = $('#form1');
				form.prop('action', '${fn:url('/CustomerApply/allotmentToken.action')}');
				form.submit();
			}	
		}
	}
</script>
<title>客戶一覽</title>
</head>
<body>
<%@ include file="/form/include/title.jsp" %>
<div id="errorForm" style="display:none">
	<s:form action="index">
		<table align="center">
			<tr>
				<td><s:property value="token_msg"/></td>
			</tr>
			<tr height="10" />
			<tr>
				<td align="center"><button type="submit">確定</button></td>
			</tr>
		</table>
	</s:form>
</div>
<s:form id="form1">
	<s:hidden name="customerList[0].is_group_main" id="is_group_main"/>
	<s:hidden name="customerList[0].is_group" id="is_group"/>
	<table class="table-style" align="center">
		<tr>
			<th>客戶ID</th>
			<th>客戶名稱</th>
			<th>AO Code</th>
			<th>集團戶</th>
			<th>TOKEN ESN</th>
			<th>OTP 狀態</th>
			<th nowrap="nowrap">TOKEN 配發日期</th>
			<th nowrap="nowrap">最近異動經辦</th>
		</tr>
		<s:iterator value="customerList" status="c" var="cus">
			<tr>
				<td><s:property value="customer_id"/></td>
				<td><s:property value="customer_name"/></td>
				<td><s:property value="ao_code"/></td>
				<s:if test='%{is_group.equals("0")}'>
					<td align="center"><s:a href="/sinopac/GroupManagement/index.action">否</s:a></td>
				</s:if>
				<s:else>
					<td align="center"><s:a href="/sinopac/GroupManagement/index.action">是</s:a></td>
				</s:else>
				<s:if test="customerList[0].token_esn!=null">
					<td><a id="token_esn" voValue="${token_esn}" href="#"><s:property value="token_esn"/></a></td>
				</s:if>
				<s:elseif test="customerList[0].token_status">
					<td align="center">配發中</td>
				</s:elseif>
				<s:elseif test='otp_status == "00" || otp_status == "03"'>
					<td align="center"><a onclick="chkApply()" href="#">未配發</a></td>
				</s:elseif>
				<s:else>
					<td>&nbsp;</td>
				</s:else>
				<td align="center"><s:property value="otp_status_msg"/></td>
				<s:if test="customerList[0].token_esn!=null">
					<td><s:property value="related_to_time"/></td>
					<td><s:property value="#session.userName"/></td>
				</s:if>
				<s:else>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
				</s:else>
			</tr>
		</s:iterator>
	</table>
	<table class="table-style" align="center">
		<tr height="20"/>
		<tr>
			<th>收件人</th>
			<th>聯絡人電話</th>
			<th>聯絡地址</th>
		</tr>
		<s:iterator value="customerList" var="c">
			<tr>
				<s:if test="token_esn == null && customerList[0].token_status==false">
					<td><input type="text" size="25" name="customer_contact" value="${ customer_contact }"/></td>
					<td><input type="text" size="25" name="customer_phone" value="${ customer_phone }"/></td>
					<td><textarea cols="60" rows="2" id="customer_address" name="customer_address">${ customer_address }</textarea></td>
				</s:if>
				<s:elseif test='customer_contact == null && customer_phone == null && customer_address == null'>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
				</s:elseif>
				<s:elseif test='customer_contact != "" || customer_phone != "" || customer_address != ""'>
					<td><s:property value="customer_contact"/></td>
					<td><s:property value="customer_phone"/></td>
					<td nowrap="nowrap"><s:property value="customer_address"/></td>
				</s:elseif>
			</tr>
		</s:iterator>
		<tr>
			<th>郵件區域</th>
			<th>郵件條碼</th>
		</tr>
		<s:iterator value="customerList" var="c">
			<tr>
				<s:if test="token_esn == null && customerList[0].token_status==false">
					<td><input type="text" size="25" name="mailarea" value="${ mailarea }"/></td>
					<td><input type="text" size="25" name="mailbarcode" value="${ mailbarcode }"/></td>
				</s:if>
				<s:elseif test='mailarea == null && mailbarcode == null'>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
				</s:elseif>
				<s:elseif test='mailarea != "" || mailbarcode != ""'>
					<td><s:property value="mailarea"/></td>
					<td><s:property value="mailbarcode"/></td>
				</s:elseif>
			</tr>
		</s:iterator>
	</table>
	<table width="700" align="center">
		<tr height="50"/>
		<tr>
			<s:if test="customerList[0].token_esn==null">
				<s:if test="customerList[0].token_status==false">
					<s:if test="customerList[0].is_group==1">
						<s:if test="customerList[0].is_group_main==1">
							<td align="center"><input type="button" name="upmd" value="更新修改"></td>
							<td width="20"></td>
						</s:if>
					</s:if>
					<s:else>
						<td align="center"><input type="button" name="upmd" value="更新修改"></td>
						<td width="20"></td>
					</s:else>
				</s:if>
			</s:if>
		</tr>
	</table>
</s:form>
</body>
</html>